package com.smallcr.interceptor;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.codec.CharEncoding;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.smallcr.annotation.IgnoreAuth;
import com.smallcr.constant.HttpStatusEnum;
import com.smallcr.doc.SessionKeys;
import com.smallcr.utils.RequestContextHolder;
import com.smallcr.utils.ResponseBodyResult;
import com.smallcr.utils.ResponseResult;
import com.smallcr.utils.StringUtils;


public class UserAccessInterceptor implements HandlerInterceptor {

	/**
	 *@Title: CheckMethodAnnotation
	 *@Description: 检查方法是否含有IgnoreAuth注解
	 *@param:   handler
	 *@return: boolean
	 *@throws
	 */
	protected boolean checkMethodAnnotation(Object handler) {

		if (!(handler instanceof HandlerMethod)) {
			return true;
		}

		HandlerMethod handlerMethod = (HandlerMethod) handler;
		IgnoreAuth ignoreAuth = handlerMethod.getMethodAnnotation(IgnoreAuth.class);
		
		/** 如果有@IgnoreAuth注解，则不验证token **/
		if (ignoreAuth != null) {
			return true;
		}
		return false;
	}

	/**
	 *@Title: checkTokenIsNull
	 *@Description: 检查token是否为空
	 *@param:  token
	 *@return: boolean
	 *@throws
	 */
	protected boolean checkTokenIsNull() {
		HttpServletRequest request = RequestContextHolder.getHttpRequest();
		String token = request.getHeader(SessionKeys.TOKEN.toString());
		
		String appCode = request.getHeader(SessionKeys.APP_CODE.toString());
		if (StringUtils.isEmpty(appCode)) {
			return printErrorInfo("appCode is Null");
		}
		if (StringUtils.isEmpty(token)) {
			return printErrorInfo("Token is Null");
		}
		return true;
	}

	/**
	 *@Title: bindVersion
	 *@Description: 绑定版本号
	 *@return: void
	 *@throws
	 */
	protected void bindVersion() {
		HttpServletRequest request = RequestContextHolder.getHttpRequest();
		String version = request.getHeader(SessionKeys.VERSION.toString());
		if (StringUtils.isNotEmpty(version)) {
			request.setAttribute(SessionKeys.VERSION.toString(), version);
		}
	}

	/**
	 *@Title: printErrorInfo
	 *@Description:printErrorInfo
	 *@param:  msg
	 *@return: boolean
	 *@throws
	 */
	protected boolean printErrorInfo(String msg) {

		HttpServletResponse response = RequestContextHolder.getHttpResponse();

		response.setCharacterEncoding(CharEncoding.UTF_8);

		ResponseResult responseResult = ResponseBodyResult.returnObject(HttpStatusEnum.UNAUTHORIZED.getCode(),
				HttpStatusEnum.UNAUTHORIZED.getDescription(), msg);

		try {
			response.getWriter().write(responseResult.toString());
		} catch (IOException e) {

		} finally {
			try {
				response.getWriter().close();
			} catch (IOException e) {
			}
		}

		return false;
	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
	}

	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
	}
}
